//
//  QZCustomTabBar.m
//  shortvideoplay
//
//  Created by tikeios_002 on 2024/6/18.
//

#import "QZCustomTabBar.h"
#import "DJItemView.h"

@interface QZCustomTabBar()

//@property (nonatomic, strong) UIView *messageNoticeView;

// 装载图标和标题
@property (nonatomic, strong) NSArray *iconSelArr;
@property (nonatomic, strong) NSArray *iconNorBlackArr;
@property (nonatomic, strong) NSArray *iconNorWhiteArr;
@property (nonatomic, strong)UIView * ding;
@end

@implementation QZCustomTabBar

+ (instancetype)tabBarFrame:(CGRect)frame andTitles:(nonnull NSArray *)titles selectIndex:(NSInteger)index changeItemBlock:(nonnull void (^)(NSInteger))changeItemBlock{
    CGRect tabRect = frame;
    
    return [[QZCustomTabBar alloc] initWithFrame:tabRect andTitles:titles selectIndex:index changeItemBlock:changeItemBlock];
}
// 重写初始化方法
- (instancetype)initWithFrame:(CGRect)frame andTitles:(NSArray *)titles selectIndex:(NSInteger)index changeItemBlock:(void (^)(NSInteger))changeItemBlock
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = viewback_black;
        
        _iconNorBlackArr = @[@"ic_home_select",@"ic_mall_select",@"ic_UploadVideo",@"ic_promotion_select",@"ic_mysetting_select"];
        _iconSelArr = @[@"ic_home",@"ic_mall",@"ic_UploadVideo",@"ic_promotion",@"ic_mysetting"];
        
        [self setBackgroundImage:[UIImage new]];
        [self setShadowImage:[UIImage new]];
        self.barTintColor = [UIColor whiteColor];
        // 添加线条
        UIView * view_lineNA = [[UIView alloc] init];
        view_lineNA.frame = CGRectMake(0,0,KScreen_W,0.5);
        view_lineNA.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0];
        view_lineNA.alpha = 0.1;
        [self addSubview:view_lineNA];
        
        UIStackView *stackView = [[UIStackView alloc] init];
        stackView.axis = UILayoutConstraintAxisHorizontal;
        stackView.distribution = UIStackViewDistributionFillEqually;
        [self addSubview:stackView];
        KWeakSelf
        [stackView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(weakSelf);
        }];
        
        [titles enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            // 背景
            DJItemView *itemBgView = [[[NSBundle mainBundle] loadNibNamed:@"DJItemView" owner:nil options:nil] firstObject];
            itemBgView.itemTitle.text = titles[idx];
            [stackView addArrangedSubview:itemBgView];
            // 把每个按钮装载到容器中
            [weakSelf.itemArrays addObject:itemBgView];
            if(idx == 2){
                itemBgView.itemImgWidth.constant = 34;
                itemBgView.itemImgHeight.constant = 34;
                itemBgView.itemTitle.hidden = YES;
            }else{
                itemBgView.itemImgWidth.constant = 24;
                itemBgView.itemImgHeight.constant = 24;
                itemBgView.itemTitle.hidden = NO;
            }
        }];
        
        // 设置默认选中项
        [self selectWidthIndex:index];
    UIButton * textShowButtonTo1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        NSString * textShowButtonTitleTo1 = @"测试功能";
        [textShowButtonTo1 setTitle:textShowButtonTitleTo1 forState:UIControlStateNormal];
        NSInteger textShowArcTo1 = arc4random_uniform(22);
        NSLog(@"随机数：%ld", (long)textShowArcTo1);
        
        CGFloat w = KScreen_W/4;
        _ding = [[UIView alloc]initWithFrame:CGRectMake(w*3+w/2+15, 2, 8, 8)];
        _ding.backgroundColor = [UIColor redColor];
        _ding.layer.cornerRadius = 4;
        _ding.clipsToBounds = YES;
        _ding.hidden = YES;
        [self addSubview:_ding];
        

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(feedbackTotalNotifitiaon) name:@"feedbackTotalNotifitiaon" object:nil];
    }
    return self;
}
- (void)feedbackTotalNotifitiaon{
    if([[QZUserInfoModel share].feedbackTotal intValue] > 0){
        _ding.hidden = NO;
    }else{
        _ding.hidden = YES;
    }
}

// 点击后处理
- (void)selectWidthIndex:(NSInteger)index{
    if(index == 2){
        return;
    }
    
    [self.itemArrays enumerateObjectsUsingBlock:^(UIView *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (![obj isKindOfClass:[DJItemView class]]) return;
            UILabel *selectLabel =((DJItemView *)obj).itemTitle;//[obj viewWithTag:2];
            UIImageView *imageView = ((DJItemView *)obj).itemImg;//[obj viewWithTag:3];
            if(index==idx){
                // 选中效果
                selectLabel.textColor = [UIColor whiteColor];
                imageView.image = KImageNamed(_iconSelArr[idx]);
            }
            else{
                // 默认效果
                selectLabel.textColor = ColorHex_Alpha(@"ffffff", 0.72);
                imageView.image = KImageNamed(_iconNorBlackArr[idx]);
            }
    }];
}

- (void)setDefautSelectIndex:(NSInteger)defautIndex{
    [self selectWidthIndex:defautIndex];
}

- (void)tabBarMessageValueWithIndex:(NSInteger)index value:(NSInteger)value{
    if (![_itemArrays[index] isKindOfClass:[DJItemView class]]) return;
    if (index < _itemArrays.count) {
        if (value > 0) {
            ((DJItemView *)_itemArrays[index]).itemNumBg.hidden = NO;
            ((DJItemView *)_itemArrays[index]).itemNum.text = (value > 99 ? @"99+" : [NSString stringWithFormat:@"%li",value]);
            if (!((DJItemView *)_itemArrays[index]).dotView.isHidden) {
                ((DJItemView *)_itemArrays[index]).dotView.hidden = YES;
            }
        } else {
            ((DJItemView *)_itemArrays[index]).itemNumBg.hidden = YES;
            ((DJItemView *)_itemArrays[index]).itemNum.text = @"0";
        }
    }
}


- (void)tabBarDotWithIndex:(NSInteger)index isShow:(BOOL)isShow {
    if (![_itemArrays[index] isKindOfClass:[DJItemView class]]) return;
    if (index < _itemArrays.count) {
        if (((DJItemView *)_itemArrays[index]).itemNumBg.isHidden) {
            ((DJItemView *)_itemArrays[index]).dotView.hidden = !isShow;
        } else {
            ((DJItemView *)_itemArrays[index]).dotView.hidden = YES;
        }
    }
}


- (NSMutableArray *)itemArrays{
    if(!_itemArrays){
        _itemArrays = [NSMutableArray new];
    }
    return _itemArrays;
}

- (instancetype)init{
    if (self = [super init]){
        
    UIButton * textShowButtonTo1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        NSString * textShowButtonTitleTo1 = @"测试功能";
        [textShowButtonTo1 setTitle:textShowButtonTitleTo1 forState:UIControlStateNormal];
        NSInteger textShowArcTo1 = arc4random_uniform(22);
        NSLog(@"随机数：%ld", (long)textShowArcTo1);
    }
    return self;
}
@end
